// pages/order/order.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    pageSize: 10,
    pageNo: 1,
    searchLoading: false,
    searchLoadingComplete: false,
    isNullData: true,//判断是否为空数组 默认为空
    isNoResult: false,
    isNoResultTips: "空空如也~"
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    self.getOrderList();
  },
  //获取列表
  getOrderList: function () {
    let self = this;
    wx.request({
      url: `${app.globalData.host}/api/miniapps/getWXOrderList`,
      data: {
        pageSize: self.data.pageSize,
        pageNo: self.data.pageNo,
        userId: wx.getStorageSync("userId")
      },
      success: (e) => {
        let data = e.data;
        if (data.success) {
          if (data.data.length > 0) {
            for (let i = 0; i < data.data.length; i++) {
              data.data[i].CreateDate = self.dateFormat(data.data[i].CreateDate)
            }
            let orderList = self.data.isNullData ? data.data : self.data.orderList.concat(data.data)
            self.setData({
              orderList: orderList
              // searchLoading: true
            })
          } else {
            self.setData({
              searchLoadingComplete: false,
              searchLoading: true
            })
          }
          if (self.data.orderList.length <= 0) {
            self.setData({
              isNoResult: true
            })
          }
        }
      },
      fail: (error) => {
        self.setData({
          isNoResult: true,
          isNoResultTips: error.errMsg
        })
      }
    })
  },
  //将日期从2019-06-22T04:27:54.000Z转换为yyyy-mm-dd hh:mm:ss
  dateFormat: function (date) {
    let dt = "";
    let d = new Date(date);
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();
    if (h < 10) {
      h = "0" + h;
    }
    if (m < 10) {
      m = "0" + m;
    }
    if (m < 10) {
      s = "0" + s;
    }
    return date.split("T")[0] + " " + h + ":" + m + ":" + s;
  },
  //跳转到其他页面
  goOther: function () {
    wx.switchTab({
      url: '/pages/home/index'
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("上拉上拉呀")
    let self = this;
    if (self.data.searchLoading && !self.data.searchLoadingComplete) {
      self.data.pageNo++;//每次触发上拉事件，把pageCount+1 
      self.setData({
        isNullData: false
      });
      self.getOrderList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})